package org.qiaice.result;

import org.qiaice.enums.Code;

public record Result<T>(Integer code, String msg, T data) {

    private static final String OK_SUFFIX = " o(*≧▽≦)ツ┏━┓";
    private static final String NO_SUFFIX = " ε(┬┬﹏┬┬)3";

    private static String setMsg(Integer code, String msg) {
        return msg.endsWith(OK_SUFFIX) || msg.endsWith(NO_SUFFIX) ?
                msg : code.toString().startsWith("1") || code.toString().startsWith("2")
                ? msg + OK_SUFFIX : msg + NO_SUFFIX;
    }

    public static <T> Result<T> ok(T data) {
        return of(Code.OK.getCode(), Code.OK.getMsg(), data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return of(Code.OK.getCode(), msg, data);
    }

    public static <T> Result<T> no() {
        return of(Code.NO.getCode(), Code.NO.getMsg(), null);
    }

    public static <T> Result<T> no(String msg) {
        return of(Code.NO.getCode(), msg, null);
    }

    public static <T> Result<T> unauthorized(String msg) {
        return of(Code.UNAUTHORIZED.getCode(), msg, null);
    }

    public static <T> Result<T> forbidden() {
        return of(Code.FORBIDDEN.getCode(), Code.FORBIDDEN.getMsg(), null);
    }

    private static <T> Result<T> of(Integer code, String msg, T data) {
        return new Result<>(code, setMsg(code, msg), data);
    }
}
